/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.zhuike.novel.app.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.luomi.lm.ad.ADType;
import com.luomi.lm.ad.DRAgent;
import com.luomi.lm.ad.IAdSuccessBack;
import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BooksByCats;
import net.zhuike.novel.app.bean.RankingList;
import net.zhuike.novel.app.bean.Rankings;
import net.zhuike.novel.app.bean.Recommend;
import net.zhuike.novel.app.service.BookStoreService;
import net.zhuike.novel.app.ui.adapter.SubRankAdapter;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

import static android.R.id.list;

/**
 * 二级排行榜
 *
 * @author wq.
 * @date 16/9/1.
 */
public class SubRankActivity extends UeBaseActivity {

    public static final String INTENT_WEEK = "_id";
    public static final String INTENT_MONTH = "month";
    public static final String INTENT_ALL = "all";
    public static final String INTENT_TITLE = "title";

    public static void startActivity(Context context, String all, String title) {
        context.startActivity(new Intent(context, SubRankActivity.class)
                .putExtra(INTENT_ALL, all)
                .putExtra(INTENT_TITLE, title));
    }

    private String all;
    private String title;
    @Bind(R.id.tips)
    LinearLayout tips;
    @Bind(R.id.ll_layout)
    LinearLayout ll_layout;
    @Bind(R.id.subRankListView)
    ListView subRankListView;
    private Context context;
    private SubRankAdapter subRankAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_sub_rank);
        hiddenFooter();
        showGoBackBtn();
        context = this;
        all = getIntent().getStringExtra(INTENT_ALL);
        title = getIntent().getStringExtra(INTENT_TITLE).split(" ")[0];
        setTitleText(title);
        subRankListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (subRankAdapter != null) {
                    if (subRankAdapter.list.size() > 0) {
                        Rankings.BooksBean data = subRankAdapter.list.get(i);
                        Recommend.RecommendBooks recommendBooks = new Recommend.RecommendBooks();
                        recommendBooks.name = data.name;
                        recommendBooks.id = data.id;
                        recommendBooks.cover = data.cover;
                        recommendBooks.lastChapter = data.description;
//                recommendBooks.updated = data.updated;
                        BookDetailActivity.startActivity(context, data.id);
                    }
                }
            }
        });
        if (!(SharedPreferencesUtil.getInstance().getString("listEndPop") == null || SharedPreferencesUtil.getInstance().getString("listEndPop").equals(""))) {
            if (SharedPreferencesUtil.getInstance().getString("listEndPop").equals("true")) {
                DRAgent.getInstance().getOpenView(context, ADType.MESSAGE_IMGS, true, new IAdSuccessBack() {

                    @Override
                    public void onError(final String result) {

                    }

                    @Override
                    public void onClick(String result) {
                        // TODO Auto-generated method stub
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void OnSuccess(String result) {
                        // TODO Auto-generated method stub

                        // TODO Auto-generated method stub


                    }

                    @Override
                    public void OnLoadAd(View view) {
                        // TODO Auto-generated method stub
                        ll_layout.setVisibility(View.VISIBLE);
                        ll_layout.addView(view);
                    }
                });
            }else{
                ll_layout.setVisibility(View.GONE);
            }
        }else{
            ll_layout.setVisibility(View.VISIBLE);
        }

        getRankList();
    }

    public void showRankList(List<Rankings.BooksBean> books) {
        subRankAdapter = new SubRankAdapter(context, books);
        subRankListView.setAdapter(subRankAdapter);
    }

    public void getRankList() {
        BookStoreService.getRankList(context, all, new ResponseCallback<Rankings>() {
            @Override
            public void onSuccess(Rankings ranking) {
                if (ranking != null) {
                    if (ranking.list != null && ranking.list.size() > 0) {
                        tips.setVisibility(View.GONE);
                        List<Rankings.BooksBean> books = ranking.list;
                        showRankList(books);
                    } else {
                        tips.setVisibility(View.VISIBLE);
                    }
                }else{
                    tips.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                tips.setVisibility(View.VISIBLE);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("书城列表");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("书城列表");
        MobclickAgent.onPause(context);
    }
}
